***GENERATES CONTROLS FOR MEAN AND SD OF LAGGED ACHIEVEMENT OF STUDENTS IN A TEACHER-COURSE CELL TO ACCOUNT FOR TRACKING****

****TESTS FREE-RIDING USING THE SHARE OF A SUBJECT-GRADE THAT A TEACHER TEACHES AS THE MEASURE OF HOW MUCH OF AN AWARD THEY'RE RESPONSIBLE FOR***

  *OPEN STUDENT LEVEL FILE*
clear all
set mem 3g
set matsize 2000
set more off
cd "C:\teacher free riding\"

cap log close
log using tracking_controls.log, replace
set seed 7563543

*GENERATE LAG SCORE FILE
use "C:\teacher free riding\hisd_data_freeriding_b.dta", clear

drop if id == .
xtset id year

foreach subject in "math" "read" "lang" "socialstu" "science" {
  gen lstanford_sd_`subject'_scale = l.stanford_sd_`subject'_scale
 }

keep id year lstanford*
sort id year
save lag_scores.dta, replace


*GENERATE MEAN AND SD OF LAG SCORES
use "C:\teacher free riding\secondary_classes.dta"
sort id year
merge m:1 id year using lag_scores.dta
keep if _merge == 3
drop _merge


**LIMIT TO HIGH SCHOOL ASPIRE GRADES***
keep if grade >= 9 & grade <= 11

*MERGE IN ASPIRE COURSE LISTS --> WORKS WELL FOR 2006-07 & LATER BUT POORLY FOR BEFORE SO WILL NEED TO USE OLD METHOD FOR PRE-PERIOD
sort course
merge course using "C:\teacher free riding\aspire_course_list_2009.dta"
replace subject = "" if year < 2006
drop _merge
sort crs_title year
merge crs_title year using "C:\teacher free riding\pre_aspire_courses.dta"
drop if _merge == 2
replace subject = aspire_subject if year < 2006

replace course_type = ""
replace course_type = "eng" if subject == "Language Arts"
replace course_type = "read" if subject == "Reading"
replace course_type = "math" if subject == "Math"
replace course_type = "sci" if subject == "Science"
replace course_type = "soc" if subject == "Social Studies"
drop if course_type == ""

gen course_type_a = course_type
replace course_type_a = "eng/read" if course_type == "eng"
replace course_type_a = "eng/read" if course_type == "read"


foreach subject in "math" "read" "lang" "socialstu" "science" {
	rename lstanford_sd_`subject'_scale stanf_`subject'
	egen stanf_`subject'_peer_mean = mean(stanf_`subject'), by(campus course tch_number grade year)
	egen stanf_`subject'_peer_sd = sd(stanf_`subject'), by(campus course tch_number grade year)
}

*COLLAPSE TO STUDENT LEVEL BASED ON THAT STUDENT'S COURSES TAKEN IN EACH COURSE TYPE
destring tch_number, replace
collapse (mean) stanf_*_peer_*, by(id year campus course_type tch_number grade )
sort id year grade campus course_type tch_number
save peer_characteristics.dta, replace
